package com.shanghaionstar.discover;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.mag.enumtype.TirePressureIconType;
import com.onstar.cn.mag.ovd.IndividualTirePressure;
import com.onstar.cn.mag.ovd.ManagedContentDetailDTO;
import com.onstar.cn.mag.ovd.OVDStatusInfo;
import com.onstar.cn.mag.ovd.TirePressure;
import com.onstar.cn.mag.ovd.VehicleMaintenance;
import com.onstar.cn.more.entity.MyOVD;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.discover.adapter.OvdEmailAdapter;
import com.shanghaionstar.discover.iview.IOVDEmailView;
import com.shanghaionstar.discover.presenter.OVDEmailPresenter;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.weight.CommonNoDatasLayout;
import com.shanghaionstar.weight.TopLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 7828wc on 16/7/21.
 */
public class OVDEmailFragment extends BaseFragment implements IOVDEmailView {
    @FindViewById
    private TopLayout top_layout;
    @FindViewById
    private ProgressLayout progress_layout;
    @FindViewById
    private CommonNoDatasLayout no_datas_layout;
    @FindViewById
    private TextView ovd_result_tv_time_id;
    @FindViewById
    private ExpandableListView ovd_list;
    @FindViewById
    private LinearLayout datas_layout;

    private OVDEmailPresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.discover_ovd_email;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        btnBackClick(top_layout);

        presenter = new OVDEmailPresenter(((BaseActivity) getActivity()).getScheduler());
        presenter.setView(this);
        presenter.getOVDEmail();
    }

    @Override
    public void setLoading(boolean isLoading) {
        progress_layout.setVisibility(isLoading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onResponseError(OnStarException e) {
//        AlertDialogUtil.showDialogByOnstarException(onActivity(), e);
        if (e != null) {
            no_datas_layout.setHintText(R.string.no_ovd_list_data_error_e3120);
        } else {
            no_datas_layout.setHintText(R.string.no_ovd_list_data_error);
        }
        datas_layout.setVisibility(View.GONE);
        no_datas_layout.setVisibility(View.VISIBLE);
    }

    @Override
    public void onReponseSuccess(ManagedContentDetailDTO object) {
        if (object != null) {
            ovd_result_tv_time_id.setText(presenter.getOvdEmailTitle());
            View footView = LayoutInflater.from(onActivity()).inflate(R.layout.discover_ovd_vehicle_condition_footer, null);
            View headView = LayoutInflater.from(onActivity()).inflate(R.layout.discover_ovd_vehicle_condition_header, null);

            TextView tv_title = (TextView) footView.findViewById(R.id.title_tv);
            tv_title.setText(R.string.option_maintenance_information);

            List<MyOVD> ovdList = convertOVD(object);
            if (ovdList.size() >= 1) {
                ovd_list.addHeaderView(headView);
                ovd_list.setAdapter(new OvdEmailAdapter(getActivity(), ovdList));
                ovd_list.setGroupIndicator(null);
            }
            ovd_list.addFooterView(footView);
            processMaintance(footView,object);

        } else {
            no_datas_layout.setHintText(R.string.no_ovd_list_data_error);
            datas_layout.setVisibility(View.GONE);
            no_datas_layout.setVisibility(View.VISIBLE);
        }
    }

    private List<MyOVD> convertOVD(ManagedContentDetailDTO dto) {
        if (dto != null && dto.getOvdEmailDTO() != null) {
            List<MyOVD> list = new ArrayList<MyOVD>();
            OVDStatusInfo ovdStatusInfo = dto.getOvdEmailDTO().getOvdStatusInfo();
            if (ovdStatusInfo == null) {
                return list;
            }
            MyOVD ovd = null;
            boolean isVolt = ApplicationUtil.getInstance().isVolt(); // 是否是isVolt车主
            if (!CommonUtil.isNull(ovdStatusInfo.getEngineTransmission())&&ovdStatusInfo.getDrainage().getStatus()!=-1){
                ovd = new MyOVD();
                ovd.setStatus(ovdStatusInfo.getEngineTransmission().getStatus());
                if (isVolt) {
                    ovd.setTitle(onActivity().getString(R.string.volt_engineTransmission));
                } else {
                    ovd.setTitle(onActivity().getString(R.string.engineTransmission));
                }
                // 如果显示的内容为空，就不显示内容
                if (ovdStatusInfo.getEngineTransmission().getDescription().equals("null")) {
                    ovd.setDescription(null);
                } else {
                    ovd.setDescription(ovdStatusInfo.getEngineTransmission().getDescription());
                }
                list.add(ovd);
            }

            if (!CommonUtil.isNull(ovdStatusInfo.getDrainage())&&ovdStatusInfo.getDrainage().getStatus()!=-1) {
                ovd = new MyOVD();
                ovd.setStatus(ovdStatusInfo.getDrainage().getStatus());
                if (isVolt) {
                    ovd.setTitle(onActivity().getString(R.string.volt_drainage));
                } else {
                    ovd.setTitle(onActivity().getString(R.string.drainage));
                }
                // 如果显示的内容为空，就不显示内容
                if (ovdStatusInfo.getDrainage().getDescription().equals("null")) {
                    ovd.setDescription(null);
                } else {
                    ovd.setDescription(ovdStatusInfo.getDrainage().getDescription());
                }
                list.add(ovd);
            }

            if (!CommonUtil.isNull(ovdStatusInfo.getAirBag())&&ovdStatusInfo.getAirBag().getStatus()!=-1) {
                ovd = new MyOVD();
                ovd.setStatus(ovdStatusInfo.getAirBag().getStatus());
                if (isVolt) {
                    ovd.setTitle(onActivity().getString(R.string.volt_airBag));
                } else {
                    ovd.setTitle(onActivity().getString(R.string.airBag));
                }
                // 如果显示的内容为空，就不显示内容
                if (!CommonUtil.isNull(ovdStatusInfo.getAirBag())
                        && !CommonUtil.isNull(ovdStatusInfo.getAirBag().getDescription())
                        && !ovdStatusInfo.getAirBag().getDescription().equals("null")) {
                    ovd.setDescription(ovdStatusInfo.getAirBag().getDescription());
                } else {
                    ovd.setDescription(null);
                }
                list.add(ovd);
            }

            if (!CommonUtil.isNull(ovdStatusInfo.getStabiliTrak())&&ovdStatusInfo.getStabiliTrak().getStatus()!=-1) {
                ovd = new MyOVD();
                ovd.setStatus(ovdStatusInfo.getStabiliTrak().getStatus());
                if (isVolt) {
                    ovd.setTitle(onActivity().getString(R.string.volt_stabiliTrak));
                } else {
                    ovd.setTitle(onActivity().getString(R.string.stabiliTrak));
                }
                // 如果显示的内容为空，就不显示内容
                if (ovdStatusInfo.getStabiliTrak().getDescription().equals("null")) {
                    ovd.setDescription(null);
                } else {
                    ovd.setDescription(ovdStatusInfo.getStabiliTrak().getDescription());
                }
                list.add(ovd);
            }
            if (ovdStatusInfo.getAbs() != null&&ovdStatusInfo.getStabiliTrak().getStatus()!=-1) {

                ovd = new MyOVD();
                ovd.setStatus(ovdStatusInfo.getAbs().getStatus());
                if (isVolt) {
                    ovd.setTitle(onActivity().getString(R.string.volt_abs));
                } else {
                    ovd.setTitle(onActivity().getString(R.string.abs));
                }
                // 如果显示的内容为空，就不显示内容
                if (ovdStatusInfo.getAbs().getDescription().equals("null")) {
                    ovd.setDescription(null);
                } else {
                    ovd.setDescription(ovdStatusInfo.getAbs().getDescription());
                }
                list.add(ovd);
            }

            if (!CommonUtil.isNull(ovdStatusInfo.getOnStar())&&ovdStatusInfo.getStabiliTrak().getStatus()!=-1) {

                ovd = new MyOVD();
                ovd.setStatus(ovdStatusInfo.getOnStar().getStatus());
                if (isVolt) {
                    ovd.setTitle(onActivity().getString(R.string.volt_onStar));
                } else {
                    ovd.setTitle(onActivity().getString(R.string.onStar));
                }
                // 如果显示的内容为空，就不显示内容
                if (ovdStatusInfo.getOnStar().getDescription().equals("null")) {
                    ovd.setDescription(null);
                } else {
                    ovd.setDescription(ovdStatusInfo.getOnStar().getDescription());
                }
                list.add(ovd);
            }

            if (!CommonUtil.isNull(ovdStatusInfo.getLithiumBattery())&&ovdStatusInfo.getLithiumBattery().getStatus()!=-1) {

                ovd = new MyOVD();
                ovd.setStatus(ovdStatusInfo.getLithiumBattery().getStatus());
                ovd.setTitle(onActivity().getString(R.string.volt_lithiumBattery));
                // 如果显示的内容为空，就不显示内容
                if (ovdStatusInfo.getLithiumBattery().getDescription().equals("null")) {
                    ovd.setDescription(null);
                } else {
                    ovd.setDescription(ovdStatusInfo.getLithiumBattery().getDescription());
                }
                list.add(ovd);
            }

            if (!CommonUtil.isNull(ovdStatusInfo.getDynamoelectricBrake())&&ovdStatusInfo.getDynamoelectricBrake().getStatus()!=-1) {

                ovd = new MyOVD();
                ovd.setStatus(ovdStatusInfo.getDynamoelectricBrake().getStatus());
                ovd.setTitle(onActivity().getString(R.string.volt_dynamoelectricBrake));
                // 如果显示的内容为空，就不显示内容
                if (ovdStatusInfo.getDynamoelectricBrake().getDescription().equals("null")) {
                    ovd.setDescription(null);
                } else {
                    ovd.setDescription(ovdStatusInfo.getDynamoelectricBrake().getDescription());
                }
                list.add(ovd);
            }

            if (!CommonUtil.isNull(ovdStatusInfo.getElectricDrive())&&ovdStatusInfo.getElectricDrive().getStatus()!=-1) {

                ovd = new MyOVD();
                ovd.setStatus(ovdStatusInfo.getElectricDrive().getStatus());
                ovd.setTitle(onActivity().getString(R.string.volt_electricDrive));
                // 如果显示的内容为空，就不显示内容
                if (ovdStatusInfo.getElectricDrive().getDescription().equals("null")) {
                    ovd.setDescription(null);
                } else {
                    ovd.setDescription(ovdStatusInfo.getElectricDrive().getDescription());
                }
                list.add(ovd);
            }

            return list;
        } else {
            return new ArrayList<MyOVD>();
        }
    }


    private void processMaintance(View view, ManagedContentDetailDTO dto) {

        if (dto == null) {
            return;
        }
        if (dto.getOvdEmailDTO() == null) {
            return;
        }
        if (dto.getOvdEmailDTO().getOvdMaintenanceInfo() == null) {
            return;
        }

        VehicleMaintenance vm = dto.getOvdEmailDTO().getOvdMaintenanceInfo().getVehicleMaintenance();
        if (vm != null) {
            String oil = onActivity().getString(R.string.oilHealth);
            if (Double.valueOf(vm.getEngineOilHealth()) == null)
                oil = (oil + "----%");
            else
                oil = (oil + Math.round(Double.valueOf(vm.getEngineOilHealth())) + "%");
            ((TextView) view.findViewById(R.id.oilContent)).setText(oil);

            String odm = onActivity().getString(R.string.odometer);
            if (Double.valueOf(vm.getOdometer()) == null)
                odm = (odm + "----" + onActivity().getString(R.string.trip_unit));
            else
                odm = (odm + Math.round(Double.valueOf(vm.getOdometer())) + onActivity().getString(R.string.trip_unit));
            ((TextView) view.findViewById(R.id.odemeterContent)).setText(odm);
        }
        TirePressure tirePressure = dto.getOvdEmailDTO().getOvdMaintenanceInfo().getTirePressure();
        if (ApplicationUtil.getInstance().getSupportedDiagnostic(Constants.TIRE_PRESSURE)) {
            view.findViewById(R.id.tire_layout).setVisibility(View.VISIBLE);
        } else {
            view.findViewById(R.id.tire_layout).setVisibility(View.GONE);
        }
        if (tirePressure != null) {
            if (Double.valueOf(tirePressure.getFrontPlacard()) != null) {
                ((TextView) view.findViewById(R.id.ovd_tire_front_reference))
                        .setText(Math.round(Double.valueOf(tirePressure.getFrontPlacard())) + "");
            }

            if (Double.valueOf(tirePressure.getRearPlacard()) != null) {
                ((TextView) view.findViewById(R.id.ovd_tire_rear_reference))
                        .setText(Math.round(Double.valueOf(tirePressure.getRearPlacard()))
                                + "");
            }
            if (tirePressure.getIndividualTirePressure() != null) {

                IndividualTirePressure individualTirePressure = tirePressure.getIndividualTirePressure();

                if (Double.valueOf(individualTirePressure.getFrontLeftPressure()) != null) {
                    ((TextView) view.findViewById(R.id.left_front_text_tire))
                            .setText(Math.round(Double.valueOf(individualTirePressure.getFrontLeftPressure()))
                                    + "");
                }
                if (individualTirePressure.getFrontLeftIcon() != null) {
                    ImageView left_front_statusImage = (ImageView) view.findViewById(R.id.left_front_status_tire);
                    left_front_statusImage.setVisibility(View.VISIBLE);
                    left_front_statusImage.setImageDrawable(getResources().getDrawable(getTireStatusId(
                            TirePressureIconType.fromValue(individualTirePressure.getFrontLeftIcon()))));
                }

                if (Double.valueOf(individualTirePressure.getFrontRightPressure()) != null) {
                    ((TextView) view.findViewById(R.id.right_front_text_tire))
                            .setText(Math.round(Double.valueOf(individualTirePressure.getFrontRightPressure()))
                                    + "");
                }
                if (individualTirePressure.getFrontRightIcon() != null) {
                    ImageView right_front_statusImage = (ImageView) view.findViewById(R.id.right_front_status_tire);
                    right_front_statusImage.setVisibility(View.VISIBLE);
                    right_front_statusImage.setImageDrawable(getResources().getDrawable(getTireStatusId(
                            TirePressureIconType.fromValue(individualTirePressure.getFrontRightIcon()))));
                }
                if (Double.valueOf(individualTirePressure.getRearLeftPressure()) != null) {
                    ((TextView) view.findViewById(R.id.left_rear_text_tire))
                            .setText(Math.round(Double.valueOf(individualTirePressure.getRearLeftPressure()))
                                    + "");
                }
                if (individualTirePressure.getRearLeftIcon() != null) {
                    ImageView left_rear_statusImage = (ImageView) view.findViewById(R.id.left_rear_status_tire);
                    left_rear_statusImage.setVisibility(View.VISIBLE);
                    left_rear_statusImage.setImageDrawable(getResources().getDrawable(
                            getTireStatusId(TirePressureIconType.fromValue(individualTirePressure.getRearLeftIcon()))));
                }
                if (Double.valueOf(individualTirePressure.getRearRightPressure()) != null) {
                    ((TextView) view.findViewById(R.id.right_rear_text_tire))
                            .setText(Math.round(Double.valueOf(individualTirePressure.getRearRightPressure()))
                                    + "");
                }
                if (individualTirePressure.getRearRightIcon() != null) {
                    ImageView right_rear_statusImage = (ImageView) view.findViewById(R.id.right_rear_status_tire);
                    right_rear_statusImage.setVisibility(View.VISIBLE);
                    right_rear_statusImage.setImageDrawable(getResources().getDrawable(getTireStatusId(
                            TirePressureIconType.fromValue(individualTirePressure.getRearRightIcon()))));
                }
            }

        }
    }

    private int getTireStatusId(TirePressureIconType status) {
        if (status == null) {
            return R.drawable.status_red;
        }
        switch (status) {
            case G:
                return R.drawable.status_green;//onstar_green_tire_img
            case R:
                return R.drawable.status_red;//onstar_red_tire_img
            case Y:
                return R.drawable.status_yellow;//onstar_yellow_tire_img
        }
        return R.drawable.status_red;//onstar_red_tire_img
    }

    @Override
    public Activity onActivity() {
        return getActivity();
    }
}
